package com.czl.module_service.viewmodel

import com.blankj.utilcode.util.LogUtils
import com.czl.base.base.BaseBean
import com.czl.base.base.BaseViewModel
import com.czl.base.base.MyApplication
import com.czl.base.binding.command.BindingAction
import com.czl.base.binding.command.BindingCommand
import com.czl.base.config.AppConstants
import com.czl.base.data.DataRepository
import com.czl.base.data.bean.MonthCardListBean
import com.czl.base.data.bean.tengyun.HealthReportBean
import com.czl.base.event.SingleLiveEvent
import com.czl.base.extension.ApiSubscriberHelper
import com.czl.base.util.RxThreadHelper

class HealthReportListViewModel(application: MyApplication, model: DataRepository) :
    BaseViewModel<DataRepository>(application, model) {

        var currentPage = 0
        val uc = UiChangeEvent()

        class UiChangeEvent {
            val loadCompleteEvent: SingleLiveEvent<HealthReportBean> = SingleLiveEvent()
        }

        /**
         * 跳转健康上报
         */
        val onBuyCardClickCommand: BindingCommand<Void> = BindingCommand(BindingAction {
            startActivity(AppConstants.Router.Service.F_ADD_HEALTH_REPORT)
        })

        val onLoadMoreListener: BindingCommand<Void> = BindingCommand(BindingAction {
            getHealthyAllList()
        })
        val onRefreshListener: BindingCommand<Void> = BindingCommand(BindingAction {
            currentPage = 0
            getHealthyAllList()
        })

        private fun getHealthyAllList() {
            val id = model.getUserId()
            val neighId = model.getAreaId().toInt()
            model.selectHealthyAll(currentPage + 1,10, id, neighId)
                .compose(RxThreadHelper.rxSchedulerHelper(this))
                .subscribe(object : ApiSubscriberHelper<BaseBean<HealthReportBean>>(){
                    override fun onResult(t: BaseBean<HealthReportBean>) {
                        if (t.state == 0){
                            currentPage ++
                            uc.loadCompleteEvent.postValue(t.data)
                        }else{
                            uc.loadCompleteEvent.postValue(null)
                        }
                    }

                    override fun onFailed(msg: String?) {
                        showErrorToast(msg)
                        uc.loadCompleteEvent.postValue(null)
                    }

                })
        }
}